<?php
class Admin_m extends CI_Model{
	public function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	function _check_valid_login($email, $password){
		$query = $this->db->select("email, password")
						  ->from("users")
						  ->where("email", $email)
						  ->where("password", $password)
						  ->get();
		if($query){
			return $query;
		}else{
			return FALSE;
		}	
	}
	
	function _get_user_infor($email){
		$query = $this->db->select("users.*, role.id as roleid, role.name")
						  ->from("users")
						  ->join("role", "role.id = users.role_id")
						  ->where("email", $email)
						  ->limit(1)
						  ->get();
		if($query){
			return $query;
		}else{
			return FALSE;
		}		
	}
	
	//--get user role--//

	
	//--get users--//
	public function _get_users($perpage, $offset){
		$query = $this->db->select("users.*, role.name AS role_name")
						  ->from("users")
						  ->join("role", "users.role_id = role.id", "left")
						  ->limit($perpage, $offset)
						  ->get();
		if($query){
			return $query->result_array();
		}else{
			return FALSE;
		}
	}
	
	//--count users--//
	public function _count_users(){
		$query = $this->db->select("users.*, role.name AS rolename")
						  ->from("users")
						  ->join("role", "users.role_id = role.id", "left")
						  ->get();
		if($query){
			$count = $query->num_rows();
			return $count;
		}else{
			return FALSE;
		}
	}
	
	//--get field by input--//
	function _get_field_input($table, $field, $input){
		$query = $this->db->select()
						  ->from($table)
						  ->where($field, $input)
						  ->get();
		if($query->num_rows() > 0){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	//--get role--//
    function _get_dd_data($table){
    	$query = $this->db->select()
    				      ->from($table)
    				      ->order_by('id', 'ASC')
    				      ->get();
    	$roles = array();
    	$roles[-1] = 'Sélectionnez';
    	
    	if($query->result()){
    		foreach ($query->result() as $role){
    			$roles[$role->id] = $role->name;
    		}
    	return $roles;
    	}else{
    		return FALSE;
    	}
    }
    
    //--add data to table user--//
    function add_user($addition_data){
    	$data = array();
    	$collums = array("user_name", "password", "email", "active", "created_on", "role_id");
    	foreach($collums as $field){
    		if(is_array($addition_data) && isset($addition_data[$field])){
    			$data[$field] = $addition_data[$field];
    		}
    		elseif ($this->input->post()){
    			$data[$field] = $this->input->post($field);
    		}
    	}
    	
    	$query = $this->db->insert("users", $data);
    	if($query){
    		return TRUE;
    	}else{
    		return FALSE;
    	}
    }
    
    //--add data to table user profile--//
	function add_user_profile($addition_data){
    	$data = array();
    	$collums = array("user_id", "first_name", "last_name");
    	foreach($collums as $field){
    		if(is_array($addition_data) && isset($addition_data[$field])){
    			$data[$field] = $addition_data[$field];
    		}
    		elseif ($this->input->post()){
    			$data[$field] = $this->input->post($field);
    		}
    	}
    	
    	$query = $this->db->insert("user_profile", $data);
    	if($query){
    		return TRUE;
    	}else{
    		return FALSE;
    	}
    }
    
    //--get insert id--//
    function _get_insert_id(){
    	if($this->db->insert_id()){
    		return $this->db->insert_id();	
    	}else{
    		return NULL;
    	}
    	
    }
    
    //--Get user profile--//
    function get_user_profile($user_id = null){
    	$query = $this->db->select("user_profile.*, users.user_name, users.email")
    				  	  ->from("user_profile")
    				  	  ->join("users", "user_profile.user_id = users.id", "left")
    				  	  ->where("user_profile.user_id", $user_id)
    				  	  ->limit(1)
    				  	  ->get();
    	if($query->num_rows()>0){
    		return $query;
    	}else{
    		return array();
    	}
    	
    	
    					  
    }
    
    //--get user_id--//
    function _get_user_id($id){
    	$query = $this->db->select("users.id")
    				  	  ->from("users")
    				  	  ->where("users.id", $id)
    				  	  ->get();
    	if($query->num_rows()>0){
    		return $query;
    	}else{
    		return FALSE;
    	}
    }
    
	 //--edit data of table user--//
    function edit_user($addition_data, $id){
    	$data = array();
    	$collums = array("first_name", "last_name", "DOB", "telephone", "mobilephone", "address", "company", "gender");
    	foreach($collums as $field){
    		if(is_array($addition_data) && isset($addition_data[$field])){
    			$data[$field] = $addition_data[$field];
    		}
    		elseif ($this->input->post()){
    			$data[$field] = $this->input->post($field);
    		}
    	}
    	
    	$query = $this->db->update("user_profile", $data, array("user_id"=>$id));
    	if($query){
    		return TRUE;
    	}else{
    		return FALSE;
    	}
    }
    
    //--update last login time--//
    function update_last_login($data, $user_id){
    	$query = $this->db->update("users", $data, array("id" => $user_id));
    	if($query){
    		return $query;
    	}else{
    		return FALSE;
    	}
    }
    
    //--check current active status--//
    function _check_active_status($id){
    	$query = $this->db->select("users.active")
    					  ->from("users")
    					  ->where("users.id", $id)
    					  ->limit(1)
    					  ->get();
    	if($query){
    		return $query;
    	}else{
    		return FALSE;
    	}
    }
    
    //--change active status--//
    function update_active_status($data, $id){
    	$query = $this->db->update("users", $data, array("id"=>$id));
    	if($query){
    		return $query;
    	}else{
    		return FALSE;
    	}
    }
    
    //--get old password--//
    function get_old_password($password, $id){
    	$query = $this->db->select("users.password")
    					  ->from("users")
    					  ->where("users.password", $password)
    					  ->where("users.id", $id)
    					  ->limit(1)
    					  ->get();
    	if($query){
    		return $query;
    	}else{
    		return FALSE;
    	}
    }
    
    //--change password--//
    function change_password($data, $id){
    	$query = $this->db->update("users", $data, array("id"=>$id));
    	if($query){
    		return $query;
    	}else{
    		return FALSE;
    	}
    }
    
    //--delete user--//
    function delete_user($id){
    	$query = $this->db->delete("users", array("id"=>$id));
    	if($query){
    		return $query;
    	}else{
    		return FALSE;
    	}
    }
    
    //--delete user profile--//
    function delete_user_profile($id){
    	$query = $this->db->delete("user_profile", array("user_id"=>$id));
    	if($query){
    		return $query;
    	}else{
    		return FALSE;
    	}	
    }
    
}